//******************************************************************************
//Compute extended costs for 1 product
function getExtendedCostForOneProduct(unitPrice)
{
	var shippingCost = document.getElementById('po_shipping_cost').value;
	var zollCost = document.getElementById('po_zoll_cost').value;
	var ExtendedCostsAmount = parseFloat(shippingCost) + parseFloat(zollCost);
	
	var productCount = 0;
	var productPricesSum = 0;
	var inputs = document.getElementsByTagName('input');
	for (i=0; i < inputs.length; i++) 
	{
		if (inputs[i] && inputs[i].id != null)
		{
			if (inputs[i].id.indexOf('pop_qty_') != -1) 
			{
				var productId = inputs[i].id.replace('pop_qty_', '');
				var Price = document.getElementById('pop_price_ht_' + productId).value;	
				productCount += parseInt(inputs[i].value);
				productPricesSum += parseInt(inputs[i].value) * Price;
			}
		}
	}	

	var retour = 0;
	if (kCostRepartitionMethod ==  'by_amount')
	{
		if (productPricesSum > 0)
			retour = (ExtendedCostsAmount / productPricesSum) *  unitPrice;
	}
	else
	{	
		if (productCount > 0)
			retour = ExtendedCostsAmount / productCount;
	}
	return retour;
}

//**************************************************************************************************************************************
//display margin and other information
function displayMargin(productId, itemId)
{
	var sellPrice = eval('product_price_' + productId);
	var buyPrice = document.getElementById('pop_price_ht_' + itemId).value;
	var extendedCosts = getExtendedCostForOneProduct(buyPrice);
	var ecoTaxAmount = 0;
	if (kEnableEcoTax )
		ecoTaxAmount = document.getElementById('pop_eco_tax_' + itemId).value;
	buyPrice = parseFloat(buyPrice) + parseFloat(extendedCosts) + parseFloat(ecoTaxAmount);
	var margin = '?';
	if (sellPrice > 0)
		margin = parseInt((sellPrice - buyPrice) / sellPrice * 100);
	var editLink = ' <img src="' + kEditImageUrl + '" onclick="editSellPrice(' + productId + ',' + itemId + ');">';
	var txt = sellPrice + editLink + '<br>' + margin + '%';
	document.getElementById('div_sellprice_' + productId).innerHTML = txt;
	document.getElementById('div_price_with_extended_cost_' + itemId).innerHTML = buyPrice.toFixed(2);
}

//**************************************************************************************************************************************
//open a popup menu to edit sell prices
var win;
function editSellPrice(productId, itemId)
{
	var changeRate = parseFloat(document.getElementById('po_currency_change_rate').value);
	var buyPrice = parseFloat(document.getElementById('pop_price_ht_' + itemId).value);
	var extendedCosts = getExtendedCostForOneProduct(buyPrice);
	var ecoTaxAmount = 0;
	if (kEnableEcoTax)
		parseFloat(document.getElementById('pop_eco_tax_' + itemId).value);
	buyPrice = (parseFloat(buyPrice) + parseFloat(extendedCosts) + parseFloat(ecoTaxAmount)) / changeRate;
	var sellPrice = parseFloat(eval('product_price_' + productId)) / changeRate;
	
	document.getElementById('pricer_buy_price').value = buyPrice.toFixed(2);
	document.getElementById('pricer_sell_price').value = sellPrice.toFixed(4);
	document.getElementById('pricer_sell_price_ttc').value =  (sellPrice * kDefaultProductSalePriceRate).toFixed(2);
	
	if (buyPrice > 0)
		document.getElementById('pricer_margin').value = parseInt((sellPrice - buyPrice) / sellPrice * 100);
	else
		document.getElementById('pricer_margin').value = '?';
	document.getElementById('pricer_product_id').value = productId;
	document.getElementById('pricer_item_id').value = itemId;
	
	win = new Window({className: "alphacube", title: "Pricer", width:200, height:400, destroyOnClose:true,closable:true,draggable:false, recenterAuto:true, okLabel: "close"});
	win.setContent('div_dlg_pricer', true, true);
	win.setCloseCallback(onClose);
	win.showCenter();
}

//************************************************************************************************************************************************
//hide pricer popup
function onClose()
{
	document.getElementById('div_dlg_pricer').style.display = 'none';
	return true;
}

//************************************************************************************************************************************************
//
function refreshPricer(source)
{
	var buyPrice = parseFloat(document.getElementById('pricer_buy_price').value);	
	var sellPrice = parseFloat(document.getElementById('pricer_sell_price').value);	
	var sellPriceTtc = parseFloat(document.getElementById('pricer_sell_price_ttc').value);	
	var margin = parseFloat(document.getElementById('pricer_margin').value);	
	switch(source)
	{
		case 'sell_price':
			var margin = 0;
			if (sellPrice > 0)
				margin = (sellPrice - buyPrice) / sellPrice * 100;
			document.getElementById('pricer_margin').value = margin.toFixed(2);
			document.getElementById('pricer_sell_price_ttc').value = (sellPrice*kDefaultProductSalePriceRate).toFixed(2);			
			break;
		case 'sell_price_ttc':
			document.getElementById('pricer_sell_price').value = (sellPriceTtc/kDefaultProductSalePriceRate).toFixed(2);
			var margin = ((sellPriceTtc/kDefaultProductSalePriceRate) - buyPrice) / sellPrice * 100;
			document.getElementById('pricer_margin').value = margin.toFixed(2);
			break;
		case 'margin':
			var sellPrice = buyPrice * (1 + margin / 100);
			document.getElementById('pricer_sell_price').value = sellPrice.toFixed(2);			
			document.getElementById('pricer_sell_price_ttc').value = (sellPrice*kDefaultProductSalePriceRate).toFixed(2);			
			break;
	}
}

//************************************************************************************************************************************************
//
function savePricer()
{
	
	//met a jour dans la page
	var changeRate = parseFloat(document.getElementById('po_currency_change_rate').value);
	var itemId = document.getElementById('pricer_item_id').value;
	var productId = document.getElementById('pricer_product_id').value;
	eval('product_price_' + productId + ' = (parseFloat(document.getElementById(\'pricer_sell_price\').value) * changeRate).toFixed(2)');
	displayMargin(productId, itemId);
	
	//sauvegarde le prix en ajax
	var request = new Ajax.Request(kPricerSaveUrl,
	    {
	        method:'post',
	        onSuccess: function onSuccess(transport)
	        			{
							win.destroy();
							document.getElementById('div_dlg_pricer').style.display = 'none';
	        			},
	        onFailure: function onAddressFailure() 
	        			{
	        				alert('error');
							win.destroy();
							document.getElementById('div_dlg_pricer').style.display = 'none';
	        			},
            parameters: Form.serialize(document.getElementById('div_dlg_pricer'))
	    }
    );
	
}